import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

import openSocket from 'socket.io-client';
const socket = openSocket('http://localhost:8081');

// navigator.serviceWorker.register('sw.js', { insecure: true })
const sendHeartbeat = () => {
  socket.emit('heartbeat', {
    timestamp: Date.now()
  });
};
setInterval(sendHeartbeat, 1000);
const subscribeToTimer = () => {
  // socket.on('timer', timestamp => cb(null, timestamp));
  // socket.emit('subscribeToTimer', 1000);
  socket.on('heartbeat', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
}

const onHeartbeat = (payload) => {
  console.log(payload);
};
socket.on('heartbeat', onHeartbeat);
class App extends Component {
  state = {
    timestamp: ''
  }
  render() {
    subscribeToTimer();
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;
